<?php

namespace mpend\modules\team\models;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;
use common\models\workbook\WorkbookMember;

/**
 * WorkerBalanceLogic
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class WorkerBalanceLogic extends WorkbookMember {

    /**
     * 获取类型文字列表
     */
    public function getTypeTextList() {
        return [
            WorkbookBalanceRecord::TYPE_WORK => '工钱',
            WorkbookBalanceRecord::TYPE_LOAN => '借支',
            WorkbookBalanceRecord::TYPE_BALANCE => '结算',
            WorkbookBalanceRecord::CUSTOM_TYPE_UNBALANCE => '未结',
        ];
    }

    /**
     * 获取成员记工薄记录
     */
    public function getDuration() {
        $first = $this->firstRecord;
        $begin = $end = date('Y.m.d');
        if ($first) {
            $begin = date('Y.m.d', $first->created_at);
        }
        return Yii::t('message', '{begin}-{end}', [
            'begin' => $begin,
            'end' => $end,
        ]);
    }

    /**
     * 获取成员记工薄记录
     */
    public function getBalances() {
        return $this->getBooks([
            'type' => WorkbookBalanceRecord::TYPE_BALANCE
        ])->orderBy([
            'created_at' => SORT_ASC
        ]);
    }

    /**
     * 获取成员记工薄记录
     */
    public function getCanBalance() {
        return $this->workbook && $this->workbook->canManage;
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'user',
            'role_id',
            'user_id',
            'workbook_id',
            'realname',
            'avatar',
            'abbrs',
            'occupation',
            'stats',
            'duration',
            'records' => 'balances',
            'canBalance',
        ];
    }

}
